#include "CommandDescriptor.h"

CommandDescriptorSp CommandDescriptor::Create(string pluginName, string name, string description)
{
	if ( name.empty() )
	{
		throw SArgumentException("Command descriptor name cannot be empty.");
	}

	return CommandDescriptorSp( new CommandDescriptor(pluginName, name, description) );
}

CommandDescriptor::CommandDescriptor( string pluginName, string name, string description )
{
	this->pluginName = pluginName;
	this->name = name;
	this->description = description;

	this->inputParameters = InputParameterSpVectorSp(new InputParameterSpVector);
	this->outputParameters = OutputParameterSpVectorSp(new OutputParameterSpVector);
}

InputParameterSp CommandDescriptor::GetInputParameter( int index )
{
	if ( index < 0 || index >= GetNumberOfInputParameters() )
	{
		throw SArgumentException("There is not an input parameter at this position.");
	}

	return inputParameters->at(index);
}

InputParameterSp CommandDescriptor::GetInputParameter( string key )
{
	BOOST_FOREACH(InputParameterSp ip, *inputParameters)
	{
		if ( ip ) 
		{
			if ( ip->GetName() == key )
			{
				return ip;
			}
		}
	}

	throw SArgumentException("There is not an input parameter with this name.");	
}

bool CommandDescriptor::IsInputParameterDefined( string key )
{
	BOOST_FOREACH(InputParameterSp ip, *inputParameters)
	{
		if ( ip ) 
		{
			if ( ip->GetName() == key )
			{
				return true;
			}
		}
	}

	return false;
}

OutputParameterSp CommandDescriptor::GetOutputParameter( int index )
{
	if ( index < 0 || index >= GetNumberOfOutputParameters() )
	{
		throw SArgumentException("There is not an output parameter at this position.");
	}

	return outputParameters->at(index);
}

OutputParameterSp CommandDescriptor::GetOutputParameter( string key )
{
	BOOST_FOREACH(OutputParameterSp op, *outputParameters)
	{
		if ( op ) 
		{
			if ( op->GetName() == key )
			{
				return op;
			}
		}
	}

	throw SArgumentException("There is not an output parameter with this name.");	
}

SStructTypeDescriptorSp CommandDescriptor::GetInputsTypeDescriptor()
{
	SStructTypeDescriptorSp inputsTypeDescriptor = SStructTypeDescriptor::Create(pluginName, "inputs", "inputs");

	BOOST_FOREACH(InputParameterSp par, *inputParameters)
	{
		if ( Utility::IsSimpleType( par->GetType() ) )
		{
			inputsTypeDescriptor->AddMember(par->GetName(), 
				par->GetDescription(),
				par->GetType() );
		}
		else
		{
			inputsTypeDescriptor->AddMember(par->GetName(), 
				par->GetDescription(),
				par->GetTypeDescriptor());
		}
	}

	return inputsTypeDescriptor;
}

SStructTypeDescriptorSp CommandDescriptor::GetOutputsTypeDescriptor()
{
	SStructTypeDescriptorSp outputsTypeDescriptor = SStructTypeDescriptor::Create(pluginName, "outputs", "outputs");

	BOOST_FOREACH(OutputParameterSp par, *outputParameters)
	{
		if ( Utility::IsSimpleType( par->GetType() ) )
		{
			outputsTypeDescriptor->AddMember(par->GetName(), 
				par->GetDescription(),
				par->GetType() );
		}
		else
		{
			outputsTypeDescriptor->AddMember(par->GetName(), 
				par->GetDescription(),
				par->GetTypeDescriptor());
		}
	}

	return outputsTypeDescriptor;
}

bool CommandDescriptor::IsOutputParameterDefined( string key )
{
	BOOST_FOREACH(OutputParameterSp op, *outputParameters)
	{
		if ( op ) 
		{
			if ( op->GetName() == key )
			{
				return true;
			}
		}
	}

	return false;
}